package org.nimi317.web_gis.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import org.nimi317.web_gis.Enum.UserStatus;
import org.nimi317.web_gis.form.post.Page;
import org.nimi317.web_gis.form.post.UserRolePost;
import org.nimi317.web_gis.entity.R;
import org.nimi317.web_gis.entity.User;
import org.nimi317.web_gis.service.IRoleService;
import org.nimi317.web_gis.service.IUserService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * @author thunderobot
 */
@RestController
@RequestMapping("users")
@SaCheckRole(value = {"admin"})
@SaCheckLogin
public class UserController {
    private final IRoleService irService;

    private final IUserService userService;

    public UserController(IRoleService irService, IUserService userService) {
        this.irService = irService;
        this.userService = userService;
    }

    @PostMapping("page")
    @org.nimi317.web_gis.annotation.Page
    public R<?> page(@RequestBody @Validated Page page) {
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<User> paged = userService.pageUser(page);
        return R.ok(paged);
    }

    @PostMapping("associate")
    public R<?> associate(@RequestBody UserRolePost user) {
        irService.associateRole(user.getUserId(), user.getRoleIds());
        return R.ok("角色关联成功");
    }

    @GetMapping("dict")
    public R<?> dict() {
        return R.ok(Arrays.stream(UserStatus.values()).map(UserStatus::getObject).toList());
    }
}
